/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wso.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.context.support.HttpRequestHandlerServlet;
import wso.bus.CustomerBus;
import wso.bus.UserBus;
import wso.dto.WsoCustomer;
import wso.dto.WsoUser;
import wso.global.CommonUtil;
import wso.global.Constants;

/**
 *
 * @author Violet_Ink
 */
public class CommonController implements HttpRequestHandler {

    private UserBus userBus;
    private CustomerBus customerBus;

    public void setUserBus(UserBus userBus) {
        this.userBus = userBus;
    }

    public void setCustomerBus(CustomerBus custBus) {
        this.customerBus = custBus;
    }

    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String action = request.getParameter(Constants.ACTION);
        HttpSession session = request.getSession(true);
        String url = "";

        if (action == null) {
            String path = session.getServletContext().getRealPath("/").replace("\\", "/");
            /*
             * Create users.xml file
             */
            File userxmlfile = new File(path + "WEB-INF/users.xml");
            WsoUser[] userArray = userBus.getAllUser();
            CommonUtil.marshallUser(userArray, userxmlfile);

            /*
             * Create customers.xml file
             */
//            File customerxmlfile = new File(path + "WEB-INF/customers.xml");
//            WsoCustomer[] custArray = customerBus.getAllCustomer();
//            CommonUtil.marshallCustomer(custArray, customerxmlfile);

            url = "index.jsp";
        } else {
            // Do something in here
        }

        RequestDispatcher rd = request.getRequestDispatcher(url);
        rd.forward(request, response);
    }
}
